package tr.com.netgis.log;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import tr.com.netgis.R;
import tr.com.netgis.db.NGDbHelper;
import tr.com.netgis.db.NGDbHelper.Table;
import tr.com.netgis.pojo.Message;
import tr.com.netgis.util.Utility;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

/**
 * 
 * @author NetGis
 *
 */
public class MessagesSqlCursorAdapter extends SimpleCursorAdapter {


	private NGDbHelper dbHelper;
	private Cursor currentCursor;
	private LayoutInflater inflater;
	private Context context;
	private int messageRowLayoutId;
	private SimpleDateFormat sdf = Utility.createSimpleDateFormat();
	private List<Message> selectedMessages;

	
	public MessagesSqlCursorAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to, NGDbHelper dbHelper) {
		super(context, layout, c, from, to);
		this.currentCursor = c;
		this.dbHelper = dbHelper;
		this.context = context;
		this.messageRowLayoutId = layout;
		this.selectedMessages = new ArrayList<Message>();
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}
	
	public List<Message> getSelectedMessages() {
		return selectedMessages;
	}


	@Override
	public boolean hasStableIds() {
		return true;
	}
	
	public View getView(int pos, View convertView, ViewGroup parent) {
		this.currentCursor.moveToPosition(pos);
		final Message message = NGDbHelper.getMessage(this.currentCursor);

		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(messageRowLayoutId, null);
		}

		ImageView messageType = (ImageView) convertView.findViewById(R.id.messagesRowImgType);
		TextView date = (TextView) convertView.findViewById(R.id.messagesRowTxtDate);
		TextView messageHeader = (TextView) convertView.findViewById(R.id.messagesRowTxtMessageHeader);
		TextView messageBody = (TextView) convertView.findViewById(R.id.messagesRowTxtMessageBody);
		CheckBox cbDelete = (CheckBox) convertView.findViewById(R.id.messagesDeleteRowCbDelete);
		
		if (cbDelete != null) { //message delete
			final Button btnDelete = (Button) ((Activity) this.context).findViewById(R.id.messagesDeleteBtnDelete);

			cbDelete.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (((CheckBox) v).isChecked()) {
						selectedMessages.add(message);
					} else {
						selectedMessages.remove(message);
					}
					int size = selectedMessages.size();
					btnDelete.setEnabled(size > 0);
					btnDelete.setText("Sil (" + size + ")");
				}
			});
		}
		
		if (!message.isRead()) {
			messageHeader.setTextColor(Color.BLACK);
		}
		
		if (date != null) {
			try {
				date.setText(Utility.getFriendlyTimeShort(sdf.parse(message.getTime())));
			} catch (Exception e) {
				date.setText(message.getTime());
			}
			
		}
		
		if (message.isCommand()) {
			messageType.setImageResource(R.drawable.ic_messagetype_command);			
		} else if (message.isMessage()){
			messageType.setImageResource(R.drawable.ic_messagetype_message);
		}
		messageHeader.setText(message.viewMessageHeader());
		messageBody.setText(message.viewMessageBody());

		return convertView;
	}

	public void ClearSelections() {
		this.dbHelper.clearSelections(Table.MESSAGES);
		this.currentCursor.requery();
	}


}